<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useColumns } from "./columns";
import {
  purchaseOrderListApi
} from "@/api/purchaseOrderApi";
const formRef = ref();
const tableRef = ref();
const selectRef = ref();
const {
  searchForm,
  columns,
  pagination,
  selectValue,
  tableData,
  onSure,
  onClear,
  onReset,
  onSearch,
  removeTag,
  handleSelectionChange,
  handleSizeChange,
  handleCurrentChange,
  loading,
  projectSelect
} = useColumns(selectRef, formRef, tableRef);
onMounted(()=> {
  getPurchaseOrder()
})
  // 获取入库单
  const purchaseOrderSelect = ref()
  const getPurchaseOrder = async(val?: any) => {
    if(val && val.length < 6) {
      return
    }
    const purchase: any = await purchaseOrderListApi({page: 1, perPage: 20, orderNo: val});
    purchaseOrderSelect.value = purchase?.result?.list;
  }
</script>

<template>
  <el-select
    ref="selectRef"
    v-model="selectValue"
    class="!w-[200px]"
    placeholder="多选产品"
    clearable
    multiple
    collapse-tags
    value-key="id"
    @remove-tag="removeTag"
    @clear="onClear"
  >
    <template #empty>
      <div class="m-4">
        <!-- <el-config-provider size="small"> -->
        <el-form ref="formRef" :inline="true" :model="searchForm" class="demo-form-inline " style="max-width: 750px;">
          <el-form-item >
            <el-select v-model="searchForm.projectId" class="!w-[150px]" filterable clearable :teleported="false" placeholder="请选择项目">
              <el-option
                v-for="item in projectSelect"
                :key="item.projectId"
                :value="item.projectId"
                :label="item.projectName"
              />
            </el-select>
          </el-form-item>
          <el-form-item >
            <el-select v-model="searchForm.purchaseOrderId" class="!w-[150px]" filterable clearable :teleported="false" placeholder="请选择入库单" :filter-method="getPurchaseOrder">
              <el-option
                v-for="item in purchaseOrderSelect"
                :key="item.purchaseOrderId"
                :value="item.purchaseOrderId"
                :label="item.orderNo"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="">
          	<el-input v-model="searchForm.productName" class="!w-[150px]" clearable placeholder="请输入产品名称" />
          </el-form-item>
          <el-form-item label="">
          	<el-input v-model="searchForm.productNo" class="!w-[150px]" clearable placeholder="请输入产品编号" />
          </el-form-item>
          <el-form-item prop="searchDate">
            <el-date-picker v-model="searchForm.dateRange" type="daterange" placeholder="请选择" clearable style="width: 260px" format="YYYY-MM-DD"
              value-format="YYYY-MM-DD" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"/>
          </el-form-item>
          <el-form-item label="">
          	<el-input v-model="searchForm.keyword" class="!w-[150px]" clearable placeholder="请输入关键词" />
          </el-form-item>
          <el-form-item class="float-right !mr-0">
            <el-button type="primary" text bg @click="onSearch">
              查询
            </el-button>
            <el-button text bg @click="onReset"> 重置 </el-button>
          </el-form-item>
        </el-form>

        <pure-table
          ref="tableRef"
          row-key="productId"
          alignWhole="center"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          :loading="loading"
          :data="tableData"
          :columns="columns"
          :pagination="pagination"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        />
        <el-button
          class="absolute bottom-[25px] left-[20px] w-[100px]"
          type="primary"
          size="small"
          @click="onSure"
        >
          确定
        </el-button>
        <!-- </el-config-provider> -->
      </div>
    </template>
  </el-select>
</template>
